/*
 * Copyright 2017 Baiyyy
 * @(#)HumitureService.java
 */

package com.byyy.ccts.modules.cc.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.byyy.ccts.core.common.service.impl.CommonServiceImpl;
import com.byyy.ccts.core.query.data.PageImpl;
import com.byyy.ccts.core.query.data.Pageable;
import com.byyy.ccts.core.query.data.Queryable;
import com.byyy.ccts.core.query.parse.QueryToWrapper;
import com.byyy.ccts.modules.cc.entity.ccts.TSHumiture;
import com.byyy.ccts.modules.cc.mapper.HumitureMapper;
import com.byyy.ccts.modules.cc.service.IHumitureService;
import com.byyy.ccts.modules.sys.entity.Cangku;
import com.byyy.ccts.modules.sys.entity.Dict;
import com.byyy.ccts.modules.sys.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 温湿度处理服务类
 * Created by DELL on 2018-5-7.
 */
@Transactional
@Service("humitureService")
public class HumitureServiceImpl extends CommonServiceImpl<HumitureMapper, TSHumiture> implements IHumitureService {

    /**
     * 根据设备编号以及采集时间查找重复数据
     * @param deviceId
     * @param collectionTimes
     * @return
     */
    @Override
    public List<TSHumiture> queryRepeatData(String deviceId, List<String> collectionTimes) {
        return this.baseMapper.queryRepeatData(deviceId, collectionTimes);
    }

    /**
     * 查询某个时间段内设备的温度记录
     * @param deviceIdList
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public List<TSHumiture> queryDeviceTempByTime(List<String> deviceIdList, Date beginTime, Date endTime) {
        return this.baseMapper.queryDeviceTempByTime(deviceIdList, beginTime, endTime);
    }

    /**
     * 查询某个时间段内设备的温度记录
     * @param deviceIdList
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public List<TSHumiture> queryDeviceTempByTimeUpload(List<String> deviceIdList, Date beginTime, Date endTime) {
        return this.baseMapper.queryDeviceTempByTime(deviceIdList, beginTime, endTime);
    }

    /**
     * 查询某个时间段内所有设备的温度记录
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public List<TSHumiture> queryAllDeviceTempByTime(Date beginTime, Date endTime) {
        return this.baseMapper.queryAllDeviceTempByTime(beginTime, endTime);
    }

    /**
     * 根据设备ID以及开始和结束时间查询温度列表
     * @param queryable
     * @param wrapper
     * @return
     */
    public com.byyy.ccts.core.query.data.Page<TSHumiture> humitureList(Queryable queryable, Wrapper<TSHumiture> wrapper) {
        QueryToWrapper<TSHumiture> queryToWrapper = new QueryToWrapper<>();
        queryToWrapper.parseCondition(wrapper, queryable);
        // 排序问题
        queryToWrapper.parseSort(wrapper, queryable);
        Pageable pageable = queryable.getPageable();
        com.baomidou.mybatisplus.plugins.Page<TSHumiture> page = new com.baomidou.mybatisplus.plugins.Page<TSHumiture>(
                pageable.getPageNumber(), pageable.getPageSize());
        com.baomidou.mybatisplus.plugins.Page<TSHumiture> content = humitureListPage(page, wrapper);
        return new PageImpl<TSHumiture>(content.getRecords(), queryable.getPageable(), content.getTotal());
    }

    /**
     * 根据设备ID以及开始和结束时间查询温度列表
     * @param page
     * @param wrapper
     * @return
     */
    public Page<TSHumiture> humitureListPage(Page<TSHumiture> page, Wrapper<TSHumiture> wrapper) {
        page.setRecords(baseMapper.humitureListPage(page, wrapper));
        return page;
    }

    @Override
    public List<TSHumiture> queryLastedHumitureInfo(List<String> deviceIds) {
        return baseMapper.queryLastedHumitureInfo(deviceIds);
    }

    @Override
    public List<TSHumiture> queryLastedHumitureInfoNoLimit(List<String> deviceIds) {
        return baseMapper.queryLastedHumitureInfoNoLimit(deviceIds);
    }

    @Override
    public List<TSHumiture> queryCurrentInfo() {
        List<Cangku> userCangkuList = UserUtils.getUserCangkuList();
        List<String> cangkuList = new ArrayList<>();
        for (Cangku cangku : userCangkuList) {
            cangkuList.add(cangku.getCangku());
        }
        return baseMapper.queryCurrentInfo(cangkuList);
    }

}
